package com.base.cn.platform.os.common.utils.web.thirdLogin;

import com.base.cn.platform.os.common.utils.BaseUtil;
import com.base.cn.platform.os.common.utils.http.HttpUtil;
import com.base.cn.platform.os.common.utils.user.ProfileType;
import com.google.gson.reflect.TypeToken;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import com.qq.connect.utils.QQConnectConfig;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * QQ登录工具
 *
 * @author s.li
 * @create 2018-08-30-20:20
 */
public class QQLoginUtils extends BaseUtil {
    private static Map<String,Boolean> initStatusMap = new HashMap<>();
    /**
     * 获取QQ登录跳转的路径
     * @param request HttpServletRequest
     * @param appId QQ的AppID
     * @param appKey QQ的密钥
     * @param redirectURI 回调路径
     * @return 跳转路径
     * @throws Exception
     */
    public static String getAuthorizeURL(HttpServletRequest request,String appId,String appKey,String redirectURI) throws Exception{
        QQLoginUtils.initQQConfigFile(appId,appKey,redirectURI);
        Oauth oauth = new Oauth();
        String callBack = oauth.getAuthorizeURL(request);
        return callBack;
    }

    /**
     * 初始化QQ登录配置文件
     * @param appId 应用ID
     * @param appKey 应该KEY
     * @param redirectURI 应用回调
     * @throws Exception
     */
    public static void initQQConfigFile(String appId,String appKey,String redirectURI) throws Exception{
        String strKey = appId+appKey+redirectURI;
        Boolean status = initStatusMap.get(strKey);
        if(status==null || !status.booleanValue()){
            QQConnectConfig.updateProperties("app_ID",appId);
            QQConnectConfig.updateProperties("app_KEY",appKey);
            QQConnectConfig.updateProperties("redirect_URI",redirectURI);
            initStatusMap.put(strKey,true);
        }
    }

    /**
     * 获取QQ的 unionid
     * @param accessToken
     * @return  unionid
     */
    public static String getUnionId(String accessToken)throws IOException{
        //获取unionid作为用户的唯一key
        String result = HttpUtil.doGet("https://graph.qq.com/oauth2.0/me?access_token="+accessToken+"&unionid=1");
        result = result.replaceAll("[callback\\(|\\);]","");
        result = result.trim();
        Map<String,String> resultMap = gson.fromJson(result.trim(),new TypeToken<Map<String, String>>(){}.getType());
        return resultMap.get("unionid");
    }

    /**
     * 获取第三方用户数据Map
     * @param accessToken
     * @param unionId
     * @return Map<String,Object>
     * @throws Exception
     */
    public static Map<String,Object> getQQProfileUser(String accessToken,String unionId) throws Exception{
        // 利用获取到的accessToken 去获取当前用的openid -------- start
        OpenID openIDObj = new OpenID(accessToken);
        //QQ的唯一标识串获取用户详情用
        String appId = openIDObj.getUserOpenID();
        // 利用获取到的accessToken 去获取当前用户的openid --------- end
        UserInfo qzoneUserInfo = new UserInfo(accessToken, appId);
        //得到QQ账号用户信息对象
        UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();

        return UserProfileUtils.genUserProfileMap(unionId,ProfileType.QQ.toString(),
                userInfoBean.getAvatar().getAvatarURL100(),
                userInfoBean.getNickname());
    }
}
